//
//  NSDate+Extension.swift
//  Sina
//
//  Created by 王杰 on 15/11/24.
//  Copyright © 2015年 王杰. All rights reserved.
//

import Foundation

extension NSDate {
    
    //将字符串转换为 日期对象
    class func sinaDate(dateString: String) -> NSDate? {
        let df = NSDateFormatter()
        df.dateFormat = "EEE MMM dd HH:mm:ss zzz yyyy"
        
        let date = df.dateFromString(dateString)
        
        return date
    }
    
    //将日期转换描述字符串
    func fullDescription() -> String {
        
        let calendar = NSCalendar.currentCalendar()
        
        if calendar.isDateInToday(self) {
            print("今天")
            let delta = Int(NSDate().timeIntervalSinceDate(self))
            if delta < 60 {
                return "刚刚"
            } else if delta < 60 * 60 {
                return "\(delta / 60)分钟前"
            }
            
            return "\(delta / 3600)小时前"
        }  else {
            var dateformatString = ""
            
            if calendar.isDateInYesterday(self) {
                
                dateformatString = "昨天 HH:mm"
            } else {
                
                let result = calendar.components(.Year, fromDate: self, toDate: NSDate(), options: [])
                
                if result.year == 0 {
                    
                    dateformatString = "MM-dd HH:mm"
                    
                } else {
                    
                    dateformatString = "yyyy-MM-dd HH:mm"
                }
            }
            
            let df = NSDateFormatter()
            df.dateFormat =  dateformatString
            return df.stringFromDate(self)
        }
    }
    
}